import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:sptapp/api/interface/user_api.dart';
import 'package:sptapp/api/request/response_data.dart';
import 'package:sptapp/widget/common_widget.dart';

import '../../config/const.dart';

class MinInstallerInfo extends StatefulWidget {
  final int status;
  const MinInstallerInfo({super.key, required this.status});

  @override
  State<MinInstallerInfo> createState() => _MinInstallerInfoState();
}

class _MinInstallerInfoState extends State<MinInstallerInfo> {

  TextEditingController accountController = TextEditingController();
  TextEditingController nameController = TextEditingController();
  TextEditingController phoneController = TextEditingController();
  TextEditingController addressController = TextEditingController();
  String account = '';
  String name = '';
  String phone = '';
  String address = '';
  int status = -1;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getInfo();
    status = widget.status;
  }

  _getInfo()async{
    ResponseData res = await UserAPI.getInstallInfo();
    if(res.code == 200 && res.data != null){
      setState(() {
        status = int.parse('${res.data['status'] ?? -1}');
        account = res.data['firstName'] ?? '';
        name = res.data['lastName'] ?? '';
        phone = res.data['phone'] ?? '';
        address = res.data['location'] ?? '';
        accountController.text = account;
        nameController.text = name;
        phoneController.text = phone;
        addressController.text = address;
      });
    }
  }
  _save()async{
    Map<String,dynamic> data = {
      "firstName": account,
      "lastName": name,
      "location": address,
      "phone": phone,
    };
    showLoading();
    ResponseData res = await UserAPI.installSave(data);
    hideLoading();
    if(res.code == 200){
      showToast('Success');
      Navigator.of(context).pop();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: defaultBlackBgColor,
      body: status!=0? Container(
        padding: EdgeInsets.symmetric(horizontal: 48.w),
        child: Column(
          children: [
            SizedBox(height: ScreenUtil().statusBarHeight + 40.w,),
            myCustomCenterTitleAppBar('SPT Personnel'.tr),
            SizedBox(height: 40.w,),
            Container(
              alignment: Alignment.centerLeft,
              child: Text('SPT Personnel'.tr,style: TextStyle(
                color: Colors.white,
                fontSize: 52.sp,
                fontWeight: FontWeight.w600
              ),textAlign: TextAlign.left,),
            ),
            SizedBox(height: 80.w,),
            Container(
              width: 654.w,
              height: 108.w,
              padding: EdgeInsets.symmetric(horizontal: 30.w),
              decoration: BoxDecoration(
                  border: Border.all(color: Color.fromRGBO(255, 255, 255, 0.5),width: 1.w),
                  borderRadius: BorderRadius.circular(20.w)
              ),
              child:  Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Expanded(child: TextField(
                    //控制器
                    textInputAction: TextInputAction.done,
                    controller: accountController,
                    maxLines: 1,
                    //是否自动更正
                    autocorrect: true,
                    enabled: status != 1,
                    //是否自动对焦
                    autofocus: false,
                    style: inputStyle,
                    inputFormatters: [
                    ],
                    decoration: InputDecoration(
                      contentPadding: const EdgeInsets.symmetric(vertical: 0,horizontal: 0),
                      hintText: 'First Name'.tr,
                      hintStyle: inputHitStyle,
                      counterText: "",
                      border: OutlineInputBorder(
                          borderSide: BorderSide.none),
                    ),
                    onChanged: (val){
                      setState(() {
                        account = val.trim();
                      });
                    },
                  )),
                  SizedBox(width: 20.w,),
                  Visibility(visible: account!=null&&account!.isNotEmpty,
                    child: GestureDetector(
                      onTap: (){
                        setState(() {
                          accountController.text = '';
                          account = '';
                        });
                      },
                      child: Container(
                        child: inputImg,
                      ),
                    ),
                  )
                ],
              ),
            ),
            SizedBox(height: 32.w,),

            Container(
              width: 654.w,
              height: 108.w,
              padding: EdgeInsets.symmetric(horizontal: 30.w),
              decoration: BoxDecoration(
                  border: Border.all(color: Color.fromRGBO(255, 255, 255, 0.5),width: 1.w),
                  borderRadius: BorderRadius.circular(20.w)
              ),
              child:  Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Expanded(child: TextField(
                    //控制器
                    textInputAction: TextInputAction.done,
                    controller: nameController,
                    maxLines: 1,
                    //是否自动更正
                    autocorrect: true,
                    enabled: status != 1,
                    //是否自动对焦
                    autofocus: false,
                    style: inputStyle,
                    inputFormatters: [
                    ],
                    decoration: InputDecoration(
                      contentPadding: const EdgeInsets.symmetric(vertical: 0,horizontal: 0),
                      hintText: 'Last Name'.tr,
                      hintStyle: inputHitStyle,
                      counterText: "",
                      border: OutlineInputBorder(
                          borderSide: BorderSide.none),
                    ),
                    onChanged: (val){
                      setState(() {
                        name = val.trim();
                      });
                    },
                  )),
                  SizedBox(width: 20.w,),
                  Visibility(visible: name!=null&&name!.isNotEmpty,
                    child: GestureDetector(
                      onTap: (){
                        setState(() {
                          nameController.text = '';
                          name = '';
                        });
                      },
                      child: Container(
                        child: inputImg,
                      ),
                    ),
                  )
                ],
              ),
            ),
            SizedBox(height: 32.w,),


            Container(
              width: 654.w,
              height: 108.w,
              padding: EdgeInsets.symmetric(horizontal: 30.w),
              decoration: BoxDecoration(
                  border: Border.all(color: Color.fromRGBO(255, 255, 255, 0.5),width: 1.w),
                  borderRadius: BorderRadius.circular(20.w)
              ),
              child:  Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Expanded(child: TextField(
                    //控制器
                    textInputAction: TextInputAction.done,
                    controller: addressController,
                    maxLines: 1,
                    //是否自动更正
                    autocorrect: true,
                    enabled: status != 1,
                    //是否自动对焦
                    autofocus: false,
                    style: inputStyle,
                    inputFormatters: [
                    ],
                    decoration: InputDecoration(
                      contentPadding: const EdgeInsets.symmetric(vertical: 0,horizontal: 0),
                      hintText: 'Location'.tr,
                      hintStyle: inputHitStyle,
                      counterText: "",
                      border: OutlineInputBorder(
                          borderSide: BorderSide.none),
                    ),
                    onChanged: (val){
                      setState(() {
                        address = val.trim();
                      });
                    },
                  )),
                  SizedBox(width: 20.w,),
                  Visibility(visible: address!=null&&address!.isNotEmpty,
                    child: GestureDetector(
                      onTap: (){
                        setState(() {
                          addressController.text = '';
                          address = '';
                        });
                      },
                      child: Container(
                        child: inputImg,
                      ),
                    ),
                  )
                ],
              ),
            ),
            SizedBox(height: 32.w,),
            Container(
              width: 654.w,
              height: 108.w,
              padding: EdgeInsets.symmetric(horizontal: 30.w),
              decoration: BoxDecoration(
                  border: Border.all(color: Color.fromRGBO(255, 255, 255, 0.5),width: 1.w),
                  borderRadius: BorderRadius.circular(20.w)
              ),
              child:  Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Expanded(child: TextField(
                    //控制器
                    textInputAction: TextInputAction.done,
                    controller: phoneController,
                    maxLines: 1,
                    enabled: status != 1,
                    //是否自动更正
                    autocorrect: true,
                    //是否自动对焦
                    autofocus: false,
                    style: inputStyle,
                    inputFormatters: [
                    ],
                    decoration: InputDecoration(
                      contentPadding: const EdgeInsets.symmetric(vertical: 0,horizontal: 0),
                      hintText: 'Phone'.tr,
                      hintStyle: inputHitStyle,
                      counterText: "",
                      border: OutlineInputBorder(
                          borderSide: BorderSide.none),
                    ),
                    onChanged: (val){
                      setState(() {
                        phone = val.trim();
                      });
                    },
                  )),
                  SizedBox(width: 20.w,),
                  Visibility(visible: phone!=null&&phone!.isNotEmpty,
                    child: GestureDetector(
                      onTap: (){
                        setState(() {
                          phoneController.text = '';
                          phone = '';
                        });
                      },
                      child: Container(
                        child: inputImg,
                      ),
                    ),
                  )
                ],
              ),
            ),
            SizedBox(height: 64.w,),
            Visibility(child: GestureDetector(
              onTap: (){
                if(_getIsDone()){
                  _save();
                }
              },
              child: Container(
                decoration: BoxDecoration(
                    color: _getIsDone() ? themeColor : themeBlueDisableColor,
                    borderRadius: BorderRadius.circular(8.w)
                ),
                height: 108.w,
                alignment: Alignment.center,
                child: Text('Continue'.tr,style: TextStyle(
                    color: Colors.white,
                    fontSize: 28.sp
                ),),
              ),
            ),visible: status != 1,),
          ],
        ),
      )
      :Container(
        padding: EdgeInsets.symmetric(horizontal: 48.w),
        alignment: Alignment.topCenter,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            SizedBox(height: ScreenUtil().statusBarHeight + 40.w,),
            myCustomCenterTitleAppBar('SPT Personnel'.tr),
            SizedBox(height: 140.w,),
            Container(
              child: Image.asset('assets/common/common_empty.png',width: 480.w,),
            ),
            SizedBox(height: 40.w,),
            Container(
              alignment: Alignment.center,
              child: Text('Pending Review'.tr,style: TextStyle(
                  color: Colors.white,
                  fontSize: 32.sp,
                  fontWeight: FontWeight.w500
              ),textAlign: TextAlign.left,),
            ),
            SizedBox(height: 24.w,),
            Container(
              alignment: Alignment.center,
              child: Text('Please wait a moment'.tr,style: TextStyle(
                  color: Colors.white.withAlpha(120),
                  fontSize: 32.sp,
                  fontWeight: FontWeight.w500
              ),textAlign: TextAlign.left,),
            ),
            Container(
              alignment: Alignment.center,
              child: Text('Your information is being reviewed'.tr,style: TextStyle(
                  color: Colors.white.withAlpha(120),
                  fontSize: 28.sp,
                  fontWeight: FontWeight.w500
              ),textAlign: TextAlign.left,),
            ),
          ],
        ),
      ),
    );
  }

  bool _getIsDone(){
    bool isDone = true;
    if(name == null || name.isEmpty || account ==null || account.isEmpty || address == null || address.isEmpty || phone == null || phone.isEmpty){
      return isDone = false;
    }
    return isDone;
  }

}
